package com.huonan.designpattern.retry;

import com.github.rholder.retry.*;
import com.google.common.base.Predicates;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @Description TODO
 * @Author lxp
 * @Date 2023/2/3 下午4:11
 * @Version V1.0
 **/
public class App {
    public static void main(String[] args) {
        Callable<Boolean> callable = new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                System.out.println("retry");
                throw  new IndexOutOfBoundsException();
                //return true; // do something useful here
            }
        };

        Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
                .retryIfResult(Predicates.isNull())
                .retryIfExceptionOfType(ClassNotFoundException.class)
                // .retryIfRuntimeException()
                .retryIfExceptionOfType(IndexOutOfBoundsException.class)
                .withStopStrategy(StopStrategies.stopAfterAttempt(10))
                // .withStopStrategy(StopStrategies.stopAfterAttempt(3))
                .withWaitStrategy(WaitStrategies.fixedWait(5, TimeUnit.MILLISECONDS))
                .build();

        try {
            retryer.call(callable);
        } catch (RetryException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
